<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021.3.19
 * Time: 13:42
 */

function maxProfit(&$array)
{
    if (count($array) <= 1) {
        return 0;
    }

    $dp[0][0] = 0;
    $dp[0][1] = -$array[0];

    for ($i=1;$i<count($array);$i++) {
        $dp[$i][0] = max($dp[$i-1][0],$dp[$i-1][1]+$array[$i]);
        $dp[$i][1] = max($dp[$i-1][1],$dp[$i-1][0]-$array[$i]);
    }
    return $dp[count($array)-1][0];
}

$array =  [7,1,5,3,6,4];
$res = maxProfit($array);
var_dump($res);


/**
 * leetcode地址 ：https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-ii-by-leetcode-s/
 *
 */